package org.jsping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 音乐播放 负责管理音乐的播放列表和用户的音乐请求 
 * 一般是用户通过点击超链接之后进入该servlet
 * 负责更新数据
 */
@WebServlet("/MusicPlay")
public class MusicPlay extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MusicPlay() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mid = request.getParameter("mid");  //是超连接发送过来的数据
		String content = "";
		boolean isFlag = false; 
		if(mid!=null) {
			//判断会话中是否有打开音乐页面
			String isOpen = (String)request.getSession().getAttribute("music");
			if(isOpen != null) {
				content = "yes"; /*如果是处于已经打开的页面的话,那么先发送yes过去*/
				isFlag = true;
			}else {
				isFlag = true;
				content = "no";
			}
		}
		PrintWriter out = response.getWriter();
		out.print(content);
		out.flush();
		out.close();
		//如果OK的话,那么就先判断session中的mids是否存在
		if(isFlag) {
			//获取会话中储存的mids数据
			List<Integer> mids = (ArrayList<Integer>)request.getSession().getAttribute("mids");
			if(mids!=null) {
				if(!mids.contains(new Integer(mid))) {
					mids.add(new Integer(mid));
					request.getSession().setAttribute("mids", mids);
				}
			}else {
				mids = new ArrayList<Integer>();
				mids.add(new Integer(mid));  //添加mid,并存到会话中
				request.getSession().setAttribute("mids", mids);
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
